package com.casic.modules.sys.service.impl;

import com.casic.modules.sys.dao.SysRoleMenuDao;
import com.casic.modules.sys.entity.SysRoleMenuEntity;
import com.casic.modules.sys.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 角色与菜单对应关系
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年9月18日 上午9:44:35
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;

	/** 生成主键策略 */
	public String createId() {
		return UUID.randomUUID().toString().replaceAll("\\-", "");
	}

	@Override
	@Transactional
	public void saveOrUpdate(String roleId, List<String> menuIdList) {
		//先删除角色与菜单关系
		sysRoleMenuDao.delete(roleId);
		if(menuIdList.size() == 0){
			return ;
		}
		List<SysRoleMenuEntity> dataList=new ArrayList<>();
		SysRoleMenuEntity sysRoleMenuEntity=null;
		for(String menuId:menuIdList){
			sysRoleMenuEntity=new SysRoleMenuEntity();
			sysRoleMenuEntity.setId(createId());
			sysRoleMenuEntity.setRoleId(roleId);
			sysRoleMenuEntity.setMenuId(menuId);
			dataList.add(sysRoleMenuEntity);
		}
		//保存角色与菜单关系
		Map<String, Object> map = new HashMap<>();
		map.put("menuIdList", dataList);
		sysRoleMenuDao.save(map);
	}

	@Override
	public List<String> queryMenuIdList(String roleId) {
		return sysRoleMenuDao.queryMenuIdList(roleId);
	}

	@Override
	public void deleteBatch(String[] menuIds) {
		sysRoleMenuDao.deleteBatch(menuIds);
	}

	@Override
	public void deleteBatchByRoleId(String[] roleIds) {
		sysRoleMenuDao.deleteBatchByRoleId(roleIds);
	}

	@Override
	public List<String> findByMenuId(String menuId) {
		return sysRoleMenuDao.selectByMenuId(menuId);
	}

}
